#include <iostream>
#include <vector>
#include <string>
#include <unistd.h>
#include <pthread.h>

#define NUM 5
int cnt = 1000;

pthread_mutex_t glock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t gcond = PTHREAD_COND_INITIALIZER;

void *threadrun(void *args)
{
    std::string name=static_cast<const char*>(args);
    while(true)
    {
        pthread_mutex_lock(&glock);
        pthread_cond_wait(&gcond,&glock);
        std::cout << name << " 计算: " << cnt << std::endl;
        cnt++;
        pthread_mutex_unlock(&glock);
    }
    
}

int main()
{
    std::vector<pthread_t> threads;
    for(int i=0;i<NUM;i++)
    {
        pthread_t tid;

        char *name=new char[64];
        snprintf(name,64,"thread-%d",i);
        int n=pthread_create(&tid,nullptr,threadrun,name);
        if(n!=0)
        continue;
        threads.push_back(tid);
        sleep(1);
    }
    //
    while(true)
    {
        std::cout<<"唤醒所有线程..."<<std::endl;
        pthread_cond_broadcast(&gcond);

        sleep(1);
    }
    for(auto &i:threads)
    {
        pthread_join(i,nullptr);
    }
    return 0;
}